<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
class Notification_Model extends CI_Model {
    //***************/
    //  RETRIEVAL
    //***************/
    function get_notification($role_user_id, $user_id){
        $result = array();
        
        // get all message id
        $this->db->where('role_user_id',$role_user_id);
        $this->db->where('user_id',$user_id);
        $retrieve = $this->db->get('notification');
        $rows = $retrieve->result();
        
        // retrieve sender of message
        if($retrieve->num_rows>0){
            foreach($rows as $row){
                $this->db->select('notification.* , message.*');
                $this->db->where('notification.role_user_id !=',$role_user_id);
                $this->db->where('notification.user_id !=',$user_id);
                $this->db->where('notification.message_placeholder_id',MESSAGE_OUTBOX);
                $this->db->where('notification.message_id',$row->message_id);
                $this->db->where('notification.is_read',false);
                
                $this->db->join('message', 'message.message_id = notification.message_id');
                $retrieve = $this->db->get('notification');
                $outbox = $retrieve->result();
                $package = array();
                if($retrieve->num_rows==1){
                    $package['message'] =$retrieve->row(0);
                    $package['user_message_map'] = $this->Message_Model->get_user_message_map_by_message_user($package['message']->message_id,$role_user_id,$user_id);
                    $package['user'] = $this->User_Model->get_user($package['message']->role_user_id,$package['message']->user_id);
                    array_push($result,$package);
                }
            }
        }
        return $result;
    }
    function get_all_notification($role_user_id, $user_id){
        $result = array();
        
        // get all message id
        $this->db->where('role_user_id',$role_user_id);
        $this->db->where('user_id',$user_id);
        $retrieve = $this->db->get('notification');
        $rows = $retrieve->result();
        
        // retrieve sender of message
        if($retrieve->num_rows>0){
            foreach($rows as $row){
                $this->db->select('notification.* , message.*');
                $this->db->where('notification.role_user_id !=',$role_user_id);
                $this->db->where('notification.user_id !=',$user_id);
                $this->db->where('notification.message_placeholder_id',MESSAGE_OUTBOX);
                $this->db->where('notification.message_id',$row->message_id);
                
                $this->db->join('message', 'message.message_id = notification.message_id');
                $retrieve = $this->db->get('notification');
                $outbox = $retrieve->result();
                $package = array();
                if($retrieve->num_rows==1){
                    $package['message'] =$retrieve->row(0);
                    $package['user_message_map'] = $this->Message_Model->get_user_message_map_by_message_user($package['message']->message_id,$role_user_id,$user_id);
                    $package['user'] = $this->User_Model->get_user($package['message']->role_user_id,$package['message']->user_id);
                    array_push($result,$package);
                }
            }
        }
        return $result;
    }
    //***************/
    //  INSERT
    //***************/
    public function insert_notification($message_id, $from_role_id, $from_user_id, $to_role_id, $to_user_id){
        // called when send a message,
        // create outbox and inbox
        $data = array(
           array(
              'message_id' => $message_id,
              'role_user_id' => $from_role_id,
              'user_id' => $from_user_id,
              'message_placeholder_id' => MESSAGE_OUTBOX,
              'is_read' => false
           ),
           array(
              'message_id' => $message_id,
              'role_user_id' => $to_role_id,
              'user_id' => $to_user_id,
              'message_placeholder_id' => MESSAGE_INBOX,
              'is_read' => false
           )
        ); 
        return $this->db->insert_batch('notification', $data);
    }
    
    //***************/
    //  UPDATE
    //***************/
    public function update_is_read($message_id,$role_user_id,$user_id){
        $data = array(
               'is_read' => true
            );
        $this->db->where('message_id', $message_id);
        $this->db->where('role_user_id !=', $role_user_id);
        $this->db->where('user_id !=', $user_id);
        $this->db->update('notification', $data); 
    }
    //***************/
    //  TESTING
    //***************/
    
}

